/*
 * @Description:登录/注册
 * @Author: maskMan
 * @LastEditTime: 2025-03-11 12:06:35
 */
import { jwtSign, resSend } from "../public/httpServe";
import Joint from "../mongo/joint";
import { NUser } from "../types/share";
import Mondb from "../mongo/mondb";
import { Table } from "../mongo/struct";
import { util } from "../public/util";
import { rewordWeight } from "../public/gameApp";
const express = require("express");
const router = express.Router();
//用户注册
router.post("/userReg", async (req: any, res: any) => {
  try {
    let { account, password, name, referrer, age } = req.body;
    if (!account || !password || !name || !age) {
      console.log("userReg注册参数不正确", account, password, name, referrer, age);
      return resSend(res, null, false, "注册参数不正确");
    }
    const reg: NUser.Reg = {
      account: account,
      password: password,
      name: name,
      age: age,
      referrer: referrer || null
    };
    const result = await Joint.createUser(reg);
    if (!result) {
      console.log("userReg注册失败", reg);
      return resSend(res, null, false, "账号注册失败");
    }
    if (result == -1) {
      console.log("userReg无效的推荐人账号", reg);
      return resSend(res, null, false, "无效的推荐人账号");
    }
    Joint.createActionRecord({
      account,
      creator: account,
      actionType: "用户注册",
      commit: result as any
    });
    console.log("userReg注册成功", reg);
    return resSend(res, account, true, "账号注册成功");
  } catch (error) {
    console.error("userReg系统错误", req, error);
    return resSend(res, null, false, "系统错误");
  }
});
//用户登录
router.get("/userLogin", async (req, res) => {
  try {
    const { account, password } = req.query;
    if (!account || !password) {
      console.log("userLogin登录参数不正确", account, password);
      return resSend(res, null, false, "登录参数不正确");
    }
    const result = await Joint.loginUser({ account, password });
    if (!result) {
      console.log("userLogin登录失败", account, password);
      return resSend(res, null, false, "账号登录失败");
    }
    if (result == -1) {
      console.log("userLogin账号尚未开通", account, password);
      return resSend(res, null, false, "账号尚未授权,请联系客服");
    }
    if (result == -2) {
      console.log("userLogin账号已过期", account, password);
      return resSend(res, null, false, "账号已过期,请充值续费");
    }
    if (result == -3) {
      console.log("userLogin账号已锁定", account, password);
      return resSend(res, null, false, "账号锁定状态");
    }
    Joint.createActionRecord({
      account,
      creator: account,
      actionType: "用户登录",
      commit: result as any
    });
    result.data["coinsRatio"] = rewordWeight.coinsRatio;
    return resSend(res, result.data, true, "账号登录成功", result.sign);
  } catch (error) {
    console.error("userLogin系统错误", req, error);
    return resSend(res, null, false, "系统错误");
  }
});
//管理员登录
router.get("/admin_login", async (req, res) => {
  try {
    const { password, account } = req.query;
    if (account && password) {
      const time = util.formaDate("年-月-日 时:分:秒");
      const result: any = await Mondb.findOneAndUpdate(Table.admin, { account, password }, { lastTime: time }, ["_id", "account", "AID", "ident"]);
      if (result) {
        const sign = jwtSign({
          id: result._id.toString(),
          uid: result.AID,
          roles: result.roles,
          ident: result.ident,
          account: result.account
        } as any);
        const sd = {
          account: result.account
        };
        Joint.createActionRecord({
          account,
          creator: account,
          actionType: "管理登录",
          commit: sd as any
        });
        return resSend(res, sd, true, "登录成功", sign);
      }
    }
    console.log("admin_login登录失败", account, password);
    return resSend(res, null, false, "账号登录失败");
  } catch (error) {
    console.error("admin_login系统错误", req, error);
    return resSend(res, null, false, "系统错误");
  }
});
export default router;
